<template>
  <div class="home">
    <el-form
        ref="form"
        :model="form"
        label-position="left"
        label-width="100px"
      >
        <el-form-item label="" :label-width="formLabelWidth">
          <p>
            <img v-if="image" @click="goFile" :src="image" class="avatar" />
            <i v-else @click="goFile" class="el-icon-plus avatar-uploader-icon"></i>
            <input
            v-show="false"
              type="file"
              id="fileInput"
              ref="clearFile"
              @change="fileInput($event)"
              accept="image/png,image/jpeg"
            />
          </p>
          
        </el-form-item>
            </el-form>
  </div>
</template>

<script>
import Method from '@/utils/proto'
import axios from "axios";
import {posts} from '@/api/api'
import $ from 'jquery';
export default {
  name: "Uploadimage",
  data(){
      return{
        urls:'',
        image: "",
        form:{},
        formLabelWidth:'130px'
      }
  }, 
  methods:{
    goFile(){this.$refs.clearFile.click()},
    fileInput(e) {
      const that = this
      var fileObj = e.target.files[0];
      console.log("fileObj:", fileObj.name);
      if (fileObj.type != "image/png" && fileObj.type != "image/jpeg") {
        alert("请上传正确的文件类型");
        return;
      }
      var obs_url = 'res/data/'+fileObj.name
        $.getJSON("https://s6.youmengv.com:8250", { obs: obs_url, type: fileObj.type }, (res, status) => {
          console.log(res);
          var uploadImgParam = {
              method: 'PUT',
              url: res.SignedUrl,
              headers: {
                  'Content-Type': fileObj.type
              },
              data: fileObj,
          };
          axios(uploadImgParam).then(function (response) {
              that.image = 'https://gres.youmengv.com/'+obs_url
          }).catch(function (error) {
              console.log(error);
          })
      })
    },
  },
  created:function(){
     
  }
};
</script>
<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  border: 1px dashed #d9d9d9 !important;
  font-size: 28px;
  color: #8c939d;
  width: 9rem;
  height: 9rem;
  line-height: 9rem;
  text-align: center;
}
.avatar {
  width: 9rem;
  height: 9rem;
  display: block;
}
</style>